<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        canvas{
            border: 1px solid black;
            margin: 0 auto;
            display: block;
        }
    </style>
</head>
<body>
    <canvas width="400px" height="500px" id="myCanvas">
    </canvas>    
    <!-- 画布的起始点是左上角（0,0） -->
    <script>
        //获取画布
        //获取画笔
        var paint = myCanvas.getContext('2d');
        //线性渐变
        // var grb = paint.createLinearGradient(0,0,100,100);
        // //创建线条渐变
        //addColorStop 必须有两次及以上   第一个参数：0-1   第二个参数：颜色
        // grb.addColorStop(0,'green');
        // grb.addColorStop(0.25,'pink');
        // grb.addColorStop(0.5,'blue');
        // grb.addColorStop(0.75,'orange');
        // grb.addColorStop(1,'yellow');

        // paint.fillStyle = grb;
        // paint.fillRect(0,0,100,100);

        //径向渐变
        var grb = paint.createRadialGradient(50,50,5,50,50,100);
        grb.addColorStop(0,'green');
        grb.addColorStop(0.5,'red');
        grb.addColorStop(1,'pink');
        // paint.fillStyle = grb;
        // paint.fillRect(0,0,400,400);
        // paint.lineWidth = 10;
        paint.strokeStyle = grb;
        paint.strokeRect(0,0,50,50)
       
        



    </script>
</body>
</html>